package com.jron.cqiu.kernel.implement;

import com.jron.cqiu.kernel.persistence.IMailDao;
import com.jron.cqiu.kernel.persistence.IPersistence;
import com.jron.cqiu.util.Message;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Created by Intellij IDEA.
 *
 * @Author: Yejw
 * @Date: 2008-6-18
 * @Time: 16:55:01
 */
public class MailDao implements IMailDao, Message {

    private static Log log = LogFactory.getLog(MailDao.class);

    IPersistence persistence = IPersistence.INSTANCE;
    private static String cId = "moid";
    private static String cTime = "motime";
    private static String cTable = "tmailorder";


    /**
     * 检验是否新的命令
     *
     * @param commandId
     * @param commandTime
     * @return
     */
    public boolean isNewCommand(String commandId, String commandTime) {
        String sql = FORMAT.format("SELECT * FROM ${0} WHERE ${1}=? AND ${2}=?", cTable, cId, cTime);
        List l = null;
        l = persistence.query(sql, new Object[]{commandId, commandTime});
        if (l == null) {
            createCommand();
            return false;
        }
        if (l.size() > 0) return false;
        return true;
    }

    public void signExecCommand(String commandId, String commandTime) {
        String sql = FORMAT.format("INSERT INTO ${0} (${1},${2}) VALUES (?, ?)", cTable, cId, cTime);
        persistence.update(sql, new Object[]{commandId, commandTime});
    }

    private void createCommand() {
        String sql = FORMAT.format("CREATE TABLE ${0} (${1},${2})", cTable, cId, cTime);
        try {
            persistence.update(sql);
        } catch (Exception e) {
            log.warn(e.getMessage());
        }
    }
}
